import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../models/product_model.dart';
import '../../../providers/product_provider.dart';
import '../../../services/theme_service.dart';

class ProductModelController extends GetxController {
  final ProductProvider productProvider = ProductProvider();
  final ThemeService _themeService = Get.find<ThemeService>();
  
  // 可观察变量
  final RxBool isLoading = false.obs;
  final RxBool isRefreshing = false.obs;
  final RxBool isLoadingMore = false.obs;
  final RxBool hasMoreData = true.obs;
  final RxList<Product> productModels = <Product>[].obs;
  
  // 可选的筛选条件
  final int? productStatus = Get.arguments?['status'] as int?;
  final bool? hasModel = Get.arguments?['hasModel'] as bool?;
  
  // 分页参数
  int currentPage = 1;
  final int pageSize = 10;
  
  // 滚动控制器
  late ScrollController scrollController;
  
  @override
  void onInit() {
    super.onInit();
    scrollController = ScrollController();
    scrollController.addListener(_scrollListener);
    fetchProductModels();
  }
  
  @override
  void onClose() {
    scrollController.removeListener(_scrollListener);
    scrollController.dispose();
    super.onClose();
  }
  
  // 滚动监听
  void _scrollListener() {
    if (scrollController.position.pixels >= scrollController.position.maxScrollExtent - 200 &&
        !isLoading.value && 
        !isLoadingMore.value && 
        hasMoreData.value) {
      loadMoreProductModels();
    }
  }
  
  // 获取物模型列表
  Future<void> fetchProductModels() async {
    try {
      isLoading.value = true;
      final response = await productProvider.getProductList(
        pageNum: currentPage,
        pageSize: pageSize,
        hasModel: hasModel, // 根据参数筛选物模型
        status: productStatus
      );
      productModels.assignAll(response.rows);
      
      // 判断是否还有更多数据
      hasMoreData.value = response.rows.length >= pageSize && response.total > productModels.length;
    } catch (e) {
      Get.snackbar('错误', '获取物模型列表失败: $e');
    } finally {
      isLoading.value = false;
      isRefreshing.value = false;
    }
  }
  
  // 刷新物模型列表
  Future<void> refreshProductModels() async {
    isRefreshing.value = true;
    currentPage = 1;
    hasMoreData.value = true;
    await fetchProductModels();
    return;
  }
  
  // 加载更多物模型
  Future<void> loadMoreProductModels() async {
    if (isLoadingMore.value || !hasMoreData.value) return;
    
    isLoadingMore.value = true;
    currentPage++;
    try {
      final response = await productProvider.getProductList(
        pageNum: currentPage,
        pageSize: pageSize,
        hasModel: hasModel,
        status: productStatus
      );
      
      if (response.rows.isNotEmpty) {
        productModels.addAll(response.rows);
      }
      
      // 判断是否还有更多数据
      hasMoreData.value = response.rows.length >= pageSize && response.total > productModels.length;
      
      if (response.rows.isEmpty) {
        hasMoreData.value = false;
      }
    } catch (e) {
      currentPage--; // 恢复页码
      Get.snackbar('错误', '加载更多物模型失败: $e');
    } finally {
      isLoadingMore.value = false;
    }
  }
  
  // 查看物模型详情
  void viewProductModelDetail(int? productId) {
    if (productId == null) {
      Get.snackbar('错误', '产品ID不能为空');
      return;
    }
    
    // 跳转到物模型详情页面
    Get.toNamed('/product/model/detail', arguments: {'productId': productId});
  }
  
  // 获取类型图标颜色
  Color getTypeIconColor(int? nodeType) {
    switch (nodeType) {
      case 1: // 直连设备
        return _themeService.getThemedColor(
          lightColor: Colors.blue,
          darkColor: Colors.blue.shade300,
        );
      case 2: // 网关设备
        return _themeService.getThemedColor(
          lightColor: Colors.green,
          darkColor: Colors.green.shade300,
        );
      case 3: // 子设备
        return _themeService.getThemedColor(
          lightColor: Colors.orange,
          darkColor: Colors.orange.shade300,
        );
      default:
        return _themeService.getThemedColor(
          lightColor: Colors.grey,
          darkColor: Colors.grey.shade400,
        );
    }
  }
  
  // 获取状态文本
  String getStatusText(int? status) {
    switch (status) {
      case 0:
        return '开发中';
      case 1:
        return '已发布';
      default:
        return '未知';
    }
  }
  
  // 获取状态颜色
  Color getStatusColor(int? status) {
    switch (status) {
      case 0:
        return _themeService.getThemedColor(
          lightColor: Colors.orange,
          darkColor: Colors.orange.shade300,
        );
      case 1:
        return _themeService.getThemedColor(
          lightColor: Colors.green,
          darkColor: Colors.green.shade300,
        );
      default:
        return _themeService.getThemedColor(
          lightColor: Colors.grey,
          darkColor: Colors.grey.shade400,
        );
    }
  }
} 